
const WeexMqtt = weex.requireModule("WeexMqtt");
const weexFunction = weex.requireModule('WeexFunction');

var topics = {};

export default {
  
   //订阅主题
   subscribes(pair, callback){

    var topic = this.geTopic(pair);
    WeexMqtt.subscribes([topic]);

    topics[topic] = callback;

    WeexMqtt.message((data)=>{

      var messages = JSON.parse(data.message);
      var last = messages[0];
      var increase = messages[1];
      topics[data.topic](last, increase, pair);
    });
  },

  //取消订阅
  unSubscribes(pair) {
    if (!pair) {
      return '';
    }

    var topic = this.geTopic(pair);
    WeexMqtt.unSubscribex([topic]);
    delete topics[topic];
  },

  //获取主题
  geTopic(pair) {
    if (!pair) {
      return '';
    }

    var exchangeName = pair.exchangeName.toUpperCase();
    var baseCoin = pair.baseCoin.toUpperCase();
    var counterCoin = pair.counterCoin.toUpperCase();
    var topic = 'public/market/contract/' + exchangeName + '/' + baseCoin + '/' +counterCoin;
    return topic;
  },
}